/**
 * Copyright (C) 2018 - 2020 https://github.com/LeeYan9
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.admin.controller;

import com.bifrost.annotation.CurrentAdmin;
import com.bifrost.annotation.CurrentStore;
import com.bifrost.entity.Plan;
import com.bifrost.model.ResultModel;
import com.bifrost.model.ro.PlanModel;
import com.bifrost.model.vo.PageVO;
import com.bifrost.service.AdminUserService;
import com.bifrost.service.CustomerService;
import com.bifrost.service.PlanService;
import com.bifrost.service.StoreService;
import com.bifrost.validator.group.IsAdd;
import com.bifrost.validator.group.IsUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @author Lee
 * Create Class on 2018/8/13
 * @com.bifrost.admin.controller
 * @wechat-mall 申领活动
 */
@RequestMapping("/admin/plan")
@RestController
@Api("/后台申领活动相关接口")
public class AdminPlanController {

    @Autowired
    PlanService planService;
    @Autowired
    StoreService storeService;
    @Autowired
    AdminUserService userService;
    @Autowired
    CustomerService customerService;

    @PostMapping("/page")
    public ResultModel page(@CurrentStore Long storeId, @RequestParam(value = "page",defaultValue = "1")Integer page , @RequestParam(value = "size",defaultValue = "8") Integer size ,@RequestParam(value = "planType",defaultValue = "TRYOUT")String planType , Date start , Date end , String storeIds , String status){
        Page<Plan> plans = planService.findPage(storeId, storeIds, page, size,planType, start, end, status);
        ArrayList<Map> maps = new ArrayList<>();
        plans.forEach(plan -> {
            HashMap<String, Object> map = new HashMap<>(7);
            map.put("title",plan.getTitle());
            map.put("id",plan.getPlanId());
            map.put("url",plan.getUrl());
            map.put("status",plan.getStatus());
            map.put("type",plan.getPlanType().name());
            map.put("startTime",plan.getActiveStartTime());
            map.put("endTime",plan.getActiveEndTime());
            map.put("storeId",plan.getStoreId());
            map.put("storeName",storeService.getMerchantStore(plan.getStoreId()).getStoreName());
            map.put("updateBy",plan.getUpdatedBy());
            maps.add(map);
        });
        return ResultModel.ok().put("page",new PageVO(page,size,(int)plans.getTotalElements(),maps));
    }

    @GetMapping("/detail")
    public ResultModel detail(@NotNull(message = "申领活动ID不能为空") Long planId){
        return ResultModel.ok().put("detail",planService.detail(planId));
    }

    @GetMapping("/plan/detail")
    public ResultModel planDetail(@NotNull(message = "申领活动ID不能为空") Long planId){
        return ResultModel.ok().put("detail",planService.planDetail(planId, null));
    }

    @ApiOperation("/添加活动")
    @PostMapping(value = "/add")
    public ResultModel add(@CurrentStore Long storeId , @CurrentAdmin Long userId ,  @Valid @Validated(IsAdd.class)@RequestBody  PlanModel planModel ){

        planService.save(planModel,storeService.getMerchantStore(storeId),userService.findById(userId));
        return ResultModel.ok();
    }

    @PostMapping(value = "/update")
    public ResultModel update(@Valid @Validated(IsUpdate.class) @RequestBody PlanModel planModel, @CurrentStore Long storeId , @CurrentAdmin Long userId){
        planService.save(planModel,storeService.getMerchantStore(storeId),userService.findById(userId));
        return ResultModel.ok();
    }

    @PostMapping(value = "/delete")
    public ResultModel update(Long planId,@CurrentStore Long storeId , @CurrentAdmin Long userId){
        planService.delete(planId,storeService.getMerchantStore(storeId),userService.findById(userId));
        return ResultModel.ok();
    }

    @GetMapping("/planItem/detail")
    public ResultModel itemDetail(Long planItemId ,@RequestParam(value = "page",defaultValue = "1")Integer page , @RequestParam(value = "size",defaultValue = "20") Integer size ,String status ){
        PageVO pageVO = planService.itemDetail(planItemId, page, size,status);
        return ResultModel.ok().put("page",pageVO);
    }

    @PostMapping("/hitLottery")
    public ResultModel hitLottery(@NotNull(message = "用户不能为空") Long planCustomerId , @CurrentAdmin Long userId){
        planService.hitLottery(planCustomerId , userService.findById(userId));
        return ResultModel.ok();
    }

    @GetMapping("/planItem/feedbacks")
    public ResultModel apply(@RequestParam Long planItemId ,@RequestParam(value = "page",defaultValue = "1")Integer page , @RequestParam(value = "size",defaultValue = "20") Integer size){
        return ResultModel.ok().put("data",planService.feedbacks(planItemId,page,size));
    }

}
